<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;

/**
 * 商品款类
 * @package app\admin\controller
 */
class Category extends Admin
{
    /**
    *商品款类列表
    */
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $data_list = Db::name('goods_class')->order(array('Sort' => 'ASC', 'ClassID' => 'DESC'))->select();
        foreach ($data_list as $key => $value) {
            if($value['TypeID'] > 0){
                $data_list[$key]['TypeName'] = Db::name('goods_type')->where(array('TypeID' => $value['TypeID']))->value('ClassName');
            }
        }
        $this->assign('page_title', '商品款类');

        $this->assign('menus', $this->getNestMenu($data_list));
        return $this->fetch();
    }



    


    /**
    *新增
    */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = Db::name('goods_class')->insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }

        //顶级款类
        $data_list = Db::name('goods_class')->where(array('ParentID' => '0','Status' => '1'))->column('ClassID,ClassName');

        //商品类型
        $type_list = Db::name('goods_type')->where(array('Status' => '1'))->column('TypeID,ClassName');

        //默认上级
        $ParentID  = input('param.pid');


        return ZBuilder::make('form')
            ->addFormItems([
                ['select', 'ParentID', '所属款类', '<span class="text-danger">默认顶级款类</span>', $data_list, $ParentID],
                ['select', 'TypeID', '商品类型', '<span class="text-danger">请选择商品类型</span>', $type_list], 
                ['text', 'ClassName', '分类名称'],
                ['text', 'CustomCode', '自编码'],
                ['text', 'SupplierNo', '供应商编码'],
                ['text', 'Craft', '工艺'], 
                ['text', 'Texture', '材质'],
                ['text', 'Accounting', '核算属性'],                 
                ['text', 'Sort', '排序'],
                ['radio', 'Status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],
                ['image', 'Thumb', '图片'],
            ])
            ->layout(['ParentID' => 6, 'SupplierNo' => '6', 'ClassName' => 6, 'CustomCode' => 6, 'Craft' => 6, 'Texture' => 6, 'Accounting' => 6, 'TypeID' => 6, 'Sort' => 6, 'Status' => 6,] )
            ->fetch();
    }


    /**
    *修改
    */
    public function edit($id = 0){
        if ($id === 0) return $this->error('缺少参数');

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = Db::name('goods_class')->update($data);
            if($result) {
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('修改失败');
            }
        }

        //详情
        $info      = Db::name('goods_class')->where(array('ClassID'=>$id))->find();
        

        //顶级款类
        $data_list = Db::name('goods_class')->where(array('ParentID'=>'0'))->column('ClassID,ClassName');

        //商品类型
        $type_list = Db::name('goods_type')->column('TypeID,ClassName');


        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'ClassID'],
                ['select', 'ParentID', '所属款类', '<span class="text-danger">默认顶级款类</span>', $data_list],
                ['select', 'TypeID', '商品类型', '<span class="text-danger">请选择商品类型</span>', $type_list], 
                ['text', 'ClassName', '分类名称'],
                ['text', 'CustomCode', '自编码'],
                ['text', 'SupplierNo', '供应商编码'],
                ['text', 'Craft', '工艺'], 
                ['text', 'Texture', '材质'],
                ['text', 'Accounting', '核算属性'],                 
                ['text', 'Sort', '排序'],
                ['radio', 'Status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],
                ['image', 'Thumb', '图片'],
            ])
            ->setFormData($info)
            ->layout(['ParentID' => 6, 'SupplierNo' => '6', 'ClassName' => 6, 'CustomCode' => 6, 'Craft' => 6, 'Texture' => 6, 'Accounting' => 6, 'TypeID' => 6, 'Sort' => 6, 'Status' => 6,] )
            ->fetch();
    }


    /**
    *模板
    */
    private function getNestMenu($lists = [], $max_level = 0, $pid = 0, $curr_level = 1)
    {
        $result = '';
        foreach ($lists as $key => $value) {
            if ($value['ParentID'] == $pid) {
                $disable  = $value['Status'] == 0 ? 'dd-disable' : '';

                // 组合节点
                $result .= '<li class="dd-item dd3-item '.$disable.'" data-id="'.$value['ClassID'].'">';
                $result .= '<div class="dd-handle dd3-handle">拖拽</div><div class="dd3-content">'.$value['ClassName'];
                /*if ($value['url_value'] != '') {
                    $result .= '<span class="link"><i class="fa fa-link"></i> '.$value['url_value'].'</span>';
                }*/

                $result .= ' <span style="margin-left:30px;">排序：'.$value['Sort'].'<span>';
                if($pid != 0){
                    $result .= ' <span style="margin-left:30px;">编号：'.$value['CustomCode'].'<span>';
                    $result .= ' <span style="margin-left:30px;">分类：'.$value['TypeName'].'<span>';
                    $result .= ' <span style="margin-left:30px;">供应商编码：'.$value['SupplierNo'].'<span>';
                }
                $result .= '<div class="action">';
                if($pid == 0){
                    $result .= '<a href="'.url('add', [ 'pid' => $value['ClassID']]).'" data-toggle="tooltip" data-original-title="新增子节点"><i class="list-icon fa fa-plus fa-fw"></i></a>';
                }
                $result .= '<a href="'.url('edit', ['id' => $value['ClassID']]).'" data-toggle="tooltip" data-original-title="编辑"><i class="list-icon fa fa-pencil fa-fw"></i></a>';
                if ($value['Status'] == 0) {
                    // 启用
                    $result .= '<a href="javascript:void(0);" data-ids="'.$value['ClassID'].'" class="enable" data-toggle="tooltip" data-original-title="启用"><i class="list-icon fa fa-check-circle-o fa-fw"></i></a>';
                } else {
                    // 禁用
                    $result .= '<a href="javascript:void(0);" data-ids="'.$value['ClassID'].'" class="disable" data-toggle="tooltip" data-original-title="禁用"><i class="list-icon fa fa-ban fa-fw"></i></a>';
                }
                $result .= '<a href="'.url('delete', ['id' => $value['ClassID'], 'table' => 'admin_menu']).'" data-toggle="tooltip" data-original-title="删除" class="ajax-get confirm"><i class="list-icon fa fa-times fa-fw"></i></a></div>';

                $result .= '</div>';


                if ($max_level == 0 || $curr_level != $max_level) {
                    unset($lists[$key]);
                    // 下级节点
                    $children = $this->getNestMenu($lists, $max_level, $value['ClassID'], $curr_level + 1);
                    if ($children != '') {
                        $result .= '<ol class="dd-list">'.$children.'</ol>';
                    }
                }

                $result .= '</li>';
            }
        }
        return $result;
    }


    /**
    *修改
    */
    public function save(){
        $data = $this->request->post();
        dump($data);
    }

    /**
    *启用
    */
     public function enable($record = []){
        $data   = $this->request->post();
        $result = Db::name('goods_class')->where(array('ClassID' => $data['ids']))->setField('Status', '1');
        if($result){
            $data['code'] = '修改成功';
        }else{
            $data['msg']  = '无修改';
        }

        return $data;
    }

    /**
     * 禁用
     */
    public function disable($record = []){
        $data   = $this->request->post();
        $result = Db::name('goods_class')->where(array('ClassID' => $data['ids']))->setField('Status', '0');
        if($result){
            $data['code'] = '修改成功';
        }else{
            $data['msg']  = '无修改';
        }

        return $data;
    }

    /**
    *删除
    */
    public function delete($record = []){
        $data   = input('');
        $result = Db::name('goods_class')->where(array('ClassID' => $data['id']))->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

   
}
